<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useGetProjectInfo } from '../composables/useGetProjectInfo'
import { useModifyProject } from '../composables/useModifyProject'
import { baseUrl } from '../config'

const props = defineProps<{ projectId: number }>()
const emit = defineEmits(['close'])

const { info, loading, error, fetchInfo } = useGetProjectInfo(baseUrl, { id: props.projectId })
const { modifyProject, loading: saving, error: saveError, result } = useModifyProject(baseUrl)

const name = ref('')
const comment = ref('')
const aggregatePackage = ref('')
const enumPackage = ref('')
const languageType = ref(0)

onMounted(async () => {
  await fetchInfo()
  if (info.value) {
    name.value = info.value.name
    comment.value = info.value.comment
    aggregatePackage.value = info.value.aggregatePackage
    enumPackage.value = info.value.enumPackage
    languageType.value = info.value.languageType
  }
})

async function handleSubmit() {
  await modifyProject({
    id: props.projectId,
    name: name.value,
    comment: comment.value,
    aggregatePackage: aggregatePackage.value,
    enumPackage: enumPackage.value,
    languageType: languageType.value
  })
  if (result.value && result.value.success) {
    emit('close')
  }
}
</script>

<template>
  <div class="modal-mask modal-mask-top">
    <div class="modal-panel">
      <div class="modify-project-panel">
        <h3>编辑项目</h3>
        <form @submit.prevent="handleSubmit">
          <div v-if="loading">加载中...</div>
          <div v-else>
            <div class="form-item">
              <label>项目名称：</label>
              <input v-model="name" required autofocus />
            </div>
            <div class="form-item">
              <label>项目说明：</label>
              <input v-model="comment" />
            </div>
            <div class="form-item">
              <label>聚合包名：</label>
              <input v-model="aggregatePackage" />
            </div>
            <div class="form-item">
              <label>枚举包名：</label>
              <input v-model="enumPackage" />
            </div>
            <div class="form-item">
              <label>语言类型：</label>
              <select v-model="languageType">
                <option :value="0">未知</option>
                <option :value="1">Java</option>
                <option :value="2">Csharp</option>
              </select>
            </div>
            <button type="submit" :disabled="saving">保存</button>
          </div>
        </form>
        <div v-if="error" class="error">{{ error }}</div>
        <div v-if="saveError" class="error">{{ saveError }}</div>
        <button class="close-btn" @click="emit('close')">关闭</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.modify-project-panel {
  max-width: 400px;
  margin: 0 auto;
  padding: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.form-item {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
}
label {
  margin-bottom: 4px;
  font-weight: bold;
}
input, select {
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #ddd;
}
button {
  padding: 8px 24px;
  border: none;
  border-radius: 6px;
  background: #1890ff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}
button:disabled {
  background: #bcdffb;
  cursor: not-allowed;
}
.error {
  color: #ff4d4f;
  margin-top: 8px;
}
.modal-mask-top {
  position: fixed;
  z-index: 5000;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-panel {
  background: #fff;
  border-radius: 10px;
  padding: 32px 24px 24px 24px;
  min-width: 340px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.12);
  position: relative;
  z-index: 5100;
}
.close-btn {
  margin-top: 16px;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  background: #f5222d;
  color: #fff;
  cursor: pointer;
}
.close-btn:hover {
  background: #c41d3b;
}
</style>